import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/widgets.dart';
import 'package:gameplay_flutter/lib_runtime/extension/widget/widget_wrapper.dart';
import 'package:gameplay_flutter/lib_runtime/utils/string_extend.dart';

import '../../../lib_basemodel/chat_room/flyScreen/flying_data.dart';
import '../../../resource/language/generated/l10n.dart';
import '../../lib_baseview.dart';
import 'gift_floating_content_builder.dart';

class CommonContentBuilder extends GiftFloatingContentBuilder {
  @override
  Widget build(BuildContext context, FlyingData data) {
    String names = "";
    if (data.receivers != null) {
      for (int i = 0; i < data.receivers!.length; i++) {
        if (i > 0) {
          names += "、";
        }
        names += data.receivers![i];
        if (names.length > 10) {
          names = "${names.safeSubstring(0, 10)}...";
        }
        if (i == 1 && i < data.receivers!.length) {
          names += T.current.few_users(data.receivers!.length);
          break;
        }
      }
    }

    return GestureDetector(
      onTap: () {
        // RoomPage.show(rootNavigatorState.currentContext!, data.channel.toInt());
      },
      child: Container(
        height: 92,
        alignment: Alignment.center,
        decoration: BoxDecoration(image: _getImage(data)),
        child: Stack(
          clipBehavior: Clip.none,
          alignment: Alignment.center,
          children: [
            const SizedBox.expand(),
            Row(
              mainAxisSize: MainAxisSize.max,
              children: [
                const SizedBox(width: 110),
                Expanded(
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      buildUserInfo(data),
                      const SizedBox(
                        height: 4,
                      ),
                      RichText(
                        maxLines: 1,
                        text: TextSpan(children: [
                          TextSpan(
                              text: T.current.give_to,
                              style: TextStyle(
                                  color: Color(0xFFFFD400),
                                  fontSize: 11,
                                  fontWeight: FontWeight.w400)),
                          TextSpan(
                              text: " $names",
                              // text: " ${data.carLevel.name} ",
                              style: const TextStyle(
                                  color: Colors.white,
                                  fontSize: 11,
                                  fontWeight: FontWeight.w400)),
                        ]),
                      )
                    ],
                  ),
                ),
                const SizedBox(width: 60),
              ],
            ),

            /// 赠送人的头像
            PositionedDirectional(
              start: 30,
              child: data.senderHeader != null
                  ? CachedNetworkImage(
                      imageUrl: data.senderHeader ?? "",
                      width: 37,
                      height: 37,
                      fit: BoxFit.cover,
                    ).corner(radius: 37 / 2.0)
                  : const SizedBox.shrink(),
            ),

            /// 礼物图片
            PositionedDirectional(
              end: 30,
              child: data.giftUrl != null
                  ? CachedNetworkImage(
                      imageUrl: data.giftUrl ?? "",
                      width: 37,
                      height: 37,
                      fit: BoxFit.cover,
                    ).corner(radius: 37 / 2.0)
                  : const SizedBox.shrink(),
            ),
          ],
        ),
      ),
    );
  }

  @override
  bool match(FlyingData data) {
    return true;
  }

  DecorationImage _getImage(FlyingData data) {
    // if (data.effectUrl != null) {
    //   return DecorationImage(
    //       image: NetworkImage(data.effectUrl!), fit: BoxFit.fitHeight);
    // }
    return DecorationImage(
        image: AssetImage(imagePath('room/float_gift_bg.png')),
        fit: BoxFit.contain);
  }
}
